<?php
require_once(Kohana::find_file('vendor','phprpc/phprpc_client'));
/**
 * Created by PhpStorm.
 * User: zu
 * Date: 14-10-9
 * Time: 上午10:08
 */

/**
 * Class Controller_accountsystemapitest
 * 服务器对于的客服端测试用
 */
class Controller_accountsystemapitest extends Controller
{
    /**
     * 测试用于采购单和采购明细数据同步（可删除）
     */
    public function action_client(){
        echo '##'.date("Y-m-d H:i:s")."<br/>";

        $page_size=5000;//单次查询的采购单数量
        //`id`, `purchaseorder_date`, `supplier_id`, `pay_way`, `purchase_id`, `money`, `remark`,`status`
        $fields_1=array('supplier_id');//要查询的字段，以逗号隔开
        //`purchaseorder_id`, `sku`,  `storage_position`, `quantity`, `single_price`, `tax_rate`, `money`,
        //`no_tax_money`, `tax_money`, `remark`, `transportation_expense`,  `status`, `tax_way`":
        $fields_2=array('sku');//采购单详情要查询的字段
        $condition=array();//要查询的条件
        //$condition['status']=array();//状态值，可以1个或2个
        $condition['date_from']='2014-08-01 00:00:00';//查询采购起始日期
        $condition['date_to']='2014-09-09 00:00:00';//查询采购结束日期
        //$condition['supplier_id']='';//根据供应商名字查询ID
        //$condition['purchase_id']='';//根据采购员姓名查询ID
        $condition['page_size']=$page_size;//单次查询量
        $condition['offset']=0;

        //$re3 = Accountapi::getPurchaseorders($fields,$condition);exit;
        $result=array(array(),array());
        $client=new PHPRPC_Client();
        $server_url="http://127.0.0.1:802/commonapi/accountsystemapi/getPurchaseorders";
        $client->useService($server_url);
        do{
            $temp=$client->getData($fields_1,$condition,$fields_2);
            if(isset($temp[0])&&$temp[0]){
                $result[0]=array_merge($result[0],$temp[0]);
            }
            if(isset($temp[1])&&$temp[1]){
                $result[1]=array_merge($result[1],$temp[1]);
            }
            $condition['offset']+=$page_size;
            //array_push($result,Arr::flatten($temp));
        }while(isset($temp[0])&&count($temp[0])==$page_size);
        var_dump($result);
        echo "<br/>##".date("Y-m-d H:i:s");
    }

    /**
     * 同步出库单（可删除）
     */
    public function action_client1()
    {
        //`wa_id`, `su_id`, `pu_id`, `wa_date`, `wa_man`, `sku`, `single_price`, `quantity`, `tax_way`, `tran_fee`, `type`
        //tax_price,cost

        //`id`, `warehouse_date`, `purchaseorders_id`, `supplier_id`, `sku`, `warehouse_quantity`,
        //`tax_way`, `single_price`, `money`, `tax_price`, `cost`, `transportation_expense`, `type`, `warehouse_man`

        echo '##'.date("Y-m-d H:i:s")."<br/>";

        $page_size=5000;//单次查询的采购单数量
        $fields=array('supplier_id');//要查询的字段，以逗号隔开
        $condition=array();//要查询的条件
        //$condition['status']=array();//状态值，可以1个或2个
        //$condition['sku']=array();//sku
        $condition['date_from']='2014-08-01 00:00:00';//查询采购起始日期
        $condition['date_to']='2014-09-09 00:00:00';//查询采购结束日期
        //$condition['supplier_id']='';//根据供应商名字查询ID
        //$condition['purchaseorders_id']='';//根据采购单ID
        //$condition['warehouse_man']='';//根据入库人ID
        $condition['page_size']=$page_size;//单次查询量
        $condition['offset']=0;

        //$re3 = Accountapi::getPurchaseorders($fields,$condition);exit;
        $result=array();
        $client=new PHPRPC_Client();
        $server_url="http://127.0.0.1:802/commonapi/accountsystemapi/getWarehouseorders";
        $client->useService($server_url);
        do{
            $temp=$client->getData($fields,$condition);
            $result=array_merge($result,$temp);
            $condition['offset']+=$page_size;
            //array_push($result,Arr::flatten($temp));
        }while(count($temp)==$page_size);
        var_dump($result);
        echo "<br/>##".date("Y-m-d H:i:s");
    }

    /**
     * 测试
     */
    public function action_dbtest(){
        $a=DB::select('poid','payaccountinfo_id')
            ->from('purchasesupplier')
            ->where('poid','IN',array(22021,22022,22023))
            ->execute('newerp')
            ->as_array('poid','payaccountinfo_id');
        print_r($a);exit;
        $client=new PHPRPC_Client();
        $server_url="http://127.0.0.1:802/commonapi/accountsystemapi/getPurchaseordersById";
        $client->useService($server_url);
        $temp=$client->getData();
        print_r($temp);
    }

    public function action_getUsers()
    {
        $client=new PHPRPC_Client();
        $server_url="http://127.0.0.1:801/commonapi/accountsystemapi/putUsers";
        $client->useService($server_url);
        $temp=$client->getData(array('id','name'));
        echo $client->getOutput();
        print_r($temp);
    }
}